const express = require("express")
const path = require("path")
const url = require("url")
//创建服务器
const app = express()
//第一个参数表示模板为以art结尾的文件
app.engine('html', require('express-art-template'));
//设置views对应的路径
app.set('views', path.join("page"));
app.set('view engine', 'art');

//静态目录
app.use("/page/",express.static(path.join('./page')))

const messageArray = []

app.get('/', function (req, res) {
    res.render('blog.html', {
        list: messageArray
    });
});

app.get('/send', function (req, res) {
    let ele = req.query
    //存入数组
    ele.date = new Date()
    messageArray.push(ele)
    res.redirect("/")
});


//配置post参数解析器
const bodyParser = require('body-parser')
// 创建application/json 解析器
const jsonParser = bodyParser.json()
// 创建 application/x-www-form-urlencoded 解析器
const urlencodedParser = bodyParser.urlencoded({ extended: false })

//use之后会自动根据表单提交的类型把数据转成  request里的body属性
app.use(urlencodedParser)
app.use(jsonParser)


app.post('/send', function (req, res) {
    let ele = req.body
    //存入数组

    ele.date = new Date()
    messageArray.push(ele)
    res.redirect("/")
});

app.listen(9000, function () {
    console.log("server is start...");
})

